package com.dfbz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dfbz.entity.Emp;
import com.dfbz.entity.Goods;
import com.dfbz.entity.Student;
import com.dfbz.entity.User;
import com.dfbz.mapper.EmpMapper;
import com.dfbz.mapper.GoodsMapper;
import com.dfbz.mapper.StudentMapper;
import com.dfbz.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = MyBatisPlusApplication.class)
@RunWith(SpringRunner.class)
public class Demo10_常用注解 {

    @Autowired
    private IUserService userService;

    @Test
    public void test1() throws Exception {
        User user = new User(null, "xiaohui", "0", 20);
        userService.save(user);
    }


    @Autowired
    private EmpMapper empMapper;


    @Test
    public void test2() throws Exception {
        // 只是逻辑删除(把id为1的记录的is_delete改为1)
        empMapper.deleteById(1L);
    }

    @Test
    public void test3() throws Exception {
        // 不会查询is_delete为1的记录
        List<Emp> empList = empMapper.selectList(null);
        for (Emp emp : empList) {
            System.out.println(emp);
        }
    }

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void test4() throws Exception {
        System.out.println(studentMapper.selectById(1L));
        System.out.println(studentMapper.selectById(2L));
    }


    @Test
    public void test5() throws Exception {

        QueryWrapper<Student> wrapper = Wrappers.query();
        // 搜索条件为0依旧可以搜索出来
        wrapper.eq("sex", "0");

        List<Student> stuList = studentMapper.selectList(wrapper);
        for (Student student : stuList) {
            System.out.println(student);
        }
    }


@Autowired
private GoodsMapper goodsMapper;

@Test
public void test6() throws Exception {
    Goods goods = goodsMapper.selectById(1L);
    goods.setCount(goods.getCount() - 1);

    goodsMapper.updateById(goods);      // 修改完毕后,version在本次version的基础上+1
}

}
